package Clases;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class PruebasMySQLWin {

/**
* @param args the command line arguments
*/

protected Connection conn = null;
protected Statement stmt = null;

/**

* Conecta con la base de datos 'prueba' del servidor
* MySQL local.

**/

public void conectar() throws Exception {
Class.forName("com.mysql.jdbc.Driver").newInstance();
//TODO
//this.conn = DriverManager.getConnection("jdbc:mysql://172.19.183.175:3306/prueba", "user", "password");
this.conn = DriverManager.getConnection("jdbc:mysql://localhost/isoII", "root", "");
this.stmt = conn.createStatement();
}

/**

* @throws Exception DOCUMENT ME!

*/

public void desconectar() throws Exception {
if (this.stmt != null) {
try {
this.stmt.close();
} catch (SQLException SQLE) {
SQLE.printStackTrace();
}
}
if (this.conn != null) {
try {
this.conn.close();
} catch (SQLException SQLE) {
SQLE.printStackTrace();
}
}
}
protected void consultarRegistro(Connection conn, String SQLquery) throws SQLException {

ResultSet rs = stmt.executeQuery(SQLquery);
int columns = rs.getMetaData().getColumnCount();
System.out.println("------------------------");
System.out.println();
while (rs.next()) {
for (int i = 1; i <= columns; i++) {
System.out.println(rs.getString(i));
}
System.out.println();
}
rs.close();
stmt.close();
System.out.println("------------------------");
System.out.flush();
}

public static void main(String[] args) throws Exception {
// TODO code application logic here
PruebasMySQLWin mn = new PruebasMySQLWin();

try{
mn.conectar();
System.out.println("Conexion Satisfactoria!!!");
mn.consultarRegistro(mn.conn, "SELECT * FROM User");
System.out.println("La Consulta se realizo exitosamente!!!");
mn.desconectar();

}
catch (Exception e) {
e.printStackTrace();
}
}
}